﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using QxFramework.Utilities;
using TMPro;
using System;

namespace QxFramework.Core
{

    public class InputManager: MonoSingleton<InputManager>{

        private static VirtualInput activeInput;

	    // Use this for initialization
	    void Awake () {
            activeInput = new StandaloneInput();
            for(var x = KeyCode.Keypad0; x <= KeyCode.Keypad9; x++)
            {
                UseableKeys.Add(x);
            }
            for(var x = KeyCode.UpArrow; x <= KeyCode.LeftArrow; x++)
            {
                UseableKeys.Add(x);
            }
            for(var x = KeyCode.A; x <= KeyCode.Z; x++)
            {
                UseableKeys.Add(x);
            }
            UseableKeys.Add(KeyCode.Space);
	    }
	
	    public bool GetButton(string name)
        {
            return activeInput.GetButton(name);
        }

        public bool GetButtonDown(string name)
        {
            return activeInput.GetButtonDown(name);
        }

        public bool GetButtonUp(string name)
        {
            return activeInput.GetButtonUp(name);
        }

        public float GetAxis(string name,bool raw=false)
        {
            return activeInput.GetAxis(name, raw);
        }


        public bool GetKeyDown(KeyCode key)
        {
            return activeInput.GetKeyDown(key);
        }

        public bool GetKey(KeyCode key)
        {
            return activeInput.GetKey(key);
        }

        public bool GetKeyUp(KeyCode key)
        {
            return activeInput.GetKeyUp(key);
        }

        public void SetStateMachineTriger(StateMachineEvent e)
        {
            MessageManager.Instance.Get<InputEvent>().DispatchMessage(InputEvent.StateMachine, this,new UniversalEventArgs(e));
        }
        
        public Vector3 MousePosition
        {
            get { return activeInput.MousePosition(); }
        }

        public List<KeyCode> UseableKeys = new List<KeyCode>();

        List<KeyCode> lastInvokeKeys = new List<KeyCode>();
        public void CreateFuncUpdateInfo(bool forceClear=false)
        {

            
            foreach(var x in UseableKeys)
            {
                if (GetKey(x)&&(forceClear==false))
                {
                    if (lastInvokeKeys.Contains(x)==false)
                    {
                        MessageManager.Instance.Get<GameControl.ItemFuncEvent>().DispatchMessage(GameControl.ItemFuncEvent.OnKey, this,
                            new UniversalEventArgs(
                            new Tuple<KeyCode, KeyState>(x, KeyState.Down)
                            ));
                        lastInvokeKeys.Add(x);
                    }
                    else
                    {
                        MessageManager.Instance.Get<GameControl.ItemFuncEvent>().DispatchMessage(GameControl.ItemFuncEvent.OnKey, this,
                            new UniversalEventArgs(
                            new Tuple<KeyCode, KeyState>(x, KeyState.On)
                            )) ;
                    }
                }
                else
                {
                    if (lastInvokeKeys.Contains(x))
                    {
                        MessageManager.Instance.Get<GameControl.ItemFuncEvent>().DispatchMessage(GameControl.ItemFuncEvent.OnKey, this,
                            new UniversalEventArgs(
                            new Tuple<KeyCode, KeyState>(x, KeyState.Up)
                            ));
                        lastInvokeKeys.Remove(x);
                    }
                }
            }
        }
       /* private void Update()
        {
            CreateFuncUpdateInfo();
        }*/
    }

    public enum InputEvent
    {
        StateMachine
    }
    public enum StateMachineEvent
    {
        TestEvent1,
        TestEvent2,
        UI_LoseMouseControl
    }
}